package com.wenhuitec.productlib.ui.adapter.attr;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gzwh.weblib.bean.NzProductAttrModel;
import com.wenhuitec.productlib.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AttrPickAdapter extends RecyclerView.Adapter<AttrPickAdapter.ViewHolder> {

    private List<NzProductAttrModel.Attr> pickModelList = new ArrayList<>();
    private Context context;

    private Set<String> selectedId = new HashSet<>();


    public void setPickModelList(List<NzProductAttrModel.Attr> pickModelList) {
        this.pickModelList = pickModelList;
        notifyDataSetChanged();
    }

    public AttrPickAdapter(Context context) {
        this.context = context;
    }

    public AttrPickAdapter(Context context, List<NzProductAttrModel.Attr> pickModelList) {
        this.context = context;
        this.pickModelList = pickModelList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView contextTv;
        LinearLayout pickLv;


        public ViewHolder(View view) {
            super(view);
            contextTv = (TextView) view.findViewById(R.id.contextTv);
            pickLv = view.findViewById(R.id.pickLv);

        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pick, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.contextTv.setText(pickModelList.get(position).getAttrName());


        if (selectedId.contains(pickModelList.get(position).getAttrId())) {
            holder.contextTv.setTextColor(context.getResources().getColor(R.color.white));
            holder.pickLv.setBackground(context.getResources().getDrawable(R.drawable.shape_view_selected_color));
        } else {
            holder.contextTv.setTextColor(context.getResources().getColor(R.color.black));
            holder.pickLv.setBackground(context.getResources().getDrawable(R.drawable.shape_view_color));
        }
        holder.contextTv.setOnClickListener(view -> {
            if (selectedId.contains(pickModelList.get(position).getAttrId())) {
                selectedId.remove(pickModelList.get(position).getAttrId());
            } else {
                selectedId.add(pickModelList.get(position).getAttrId());
            }
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return pickModelList.size();
    }


}
